package com.greenhouse.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.greenhouse.business.domain.BizAlarmLog;
import com.greenhouse.business.service.IBizAlarmLogService;
import com.greenhouse.common.annotation.Log;
import com.greenhouse.common.core.controller.BaseController;
import com.greenhouse.common.core.domain.AjaxResult;
import com.greenhouse.common.core.page.TableDataInfo;
import com.greenhouse.common.enums.BusinessType;
import com.greenhouse.common.utils.poi.ExcelUtil;

/**
 * 报警日志Controller
 * 
 * @author greenhouse
 * @date 2021-05-07
 */
@RestController
@RequestMapping("/business/alarmlog")
public class BizAlarmLogController extends BaseController {
	@Autowired
	private IBizAlarmLogService bizAlarmLogService;

	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 查询报警日志列表
	 */
	@PreAuthorize("@ss.hasPermi('business:log:list')")
	@GetMapping("/list")
	public TableDataInfo list(BizAlarmLog bizAlarmLog) {
		startPage();
		List<BizAlarmLog> list = bizAlarmLogService.selectBizAlarmLogList(bizAlarmLog);
		return getDataTable(list);
	}

	/**
	 * 导出报警日志列表
	 */
	@PreAuthorize("@ss.hasPermi('business:log:export')")
	@Log(title = "报警日志", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(BizAlarmLog bizAlarmLog) {
		List<BizAlarmLog> list = bizAlarmLogService.selectBizAlarmLogList(bizAlarmLog);
		ExcelUtil<BizAlarmLog> util = new ExcelUtil<BizAlarmLog>(BizAlarmLog.class);
		return util.exportExcel(list, "log");
	}

	/**
	 * 获取报警日志详细信息
	 */
	@PreAuthorize("@ss.hasPermi('business:log:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(bizAlarmLogService.selectBizAlarmLogById(id));
	}

	/**
	 * 新增报警日志
	 */
	@PreAuthorize("@ss.hasPermi('business:log:add')")
	@Log(title = "报警日志", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody BizAlarmLog bizAlarmLog) {
		return toAjax(bizAlarmLogService.insertBizAlarmLog(bizAlarmLog));
	}

	/**
	 * 修改报警日志
	 */
	@PreAuthorize("@ss.hasPermi('business:log:edit')")
	@Log(title = "报警日志", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BizAlarmLog bizAlarmLog) {
		return toAjax(bizAlarmLogService.updateBizAlarmLog(bizAlarmLog));
	}

	/**
	 * 删除报警日志
	 */
	@PreAuthorize("@ss.hasPermi('business:log:remove')")
	@Log(title = "报警日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(bizAlarmLogService.deleteBizAlarmLogByIds(ids));
	}

	/**
	 * 查询设备报警日志
	 */
	@GetMapping("/listByDeviceId/{id}")
	public AjaxResult listByDeviceId(@PathVariable Long id) {
		return AjaxResult.success(bizAlarmLogService.listByDeviceId(id));
	}

	/**
	 * 查询设备报警日志
	 */
	@GetMapping("/listByNum/{num}")
	public AjaxResult listByNum(@PathVariable int num) {
		return AjaxResult.success(bizAlarmLogService.listByNum(num));
	}

	@GetMapping("/stasticAlarmByMonth")
	public AjaxResult stasticAlarmByMonth() {
		return AjaxResult.success(bizAlarmLogService.stasticAlarmByMonth());
	}

	@GetMapping("/login")
	public AjaxResult login() {

		MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();

		paramMap.add("un", "ys");

		paramMap.add("psw", "e10adc3949ba59abbe56e057f20f883e");

		ResponseEntity<String> message = restTemplate.postForEntity("http://39.104.17.9:8083/ZHONGYI/login", paramMap,
				String.class);

		return AjaxResult.success(JSONObject.parseObject(message.getBody()));
	}

}
